﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

using SpiderCore.Spiders;
using SpiderCore.Utility;
using SpiderCore.Visited;

namespace SpiderCore.Service
{
    /// <summary>
    /// 爬虫模块入口服务
    /// </summary>
    public static partial class SpiderService
    {
        /// <summary>
        /// 添加Url到ToDo队列
        /// </summary>
        /// <param name="urls">地址</param>
        /// <param name="flag">内容标志</param>
        /// <param name="route">路由标志</param>
        /// <returns></returns>
        public static bool AddUrlToTodoQueue(string[] urls, string flag, string route)
        {
            try
            {
                foreach (var url in urls)
                {
                    if (!string.IsNullOrWhiteSpace(url))
                    {
                        ToDoData urData = new ToDoData();
                        urData.PageUrl = url;
                        urData.Flag = flag;
                        urData.Route = route;
                        ToDo.AddUrl(urData);
                    }

                }
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("添加Url到ToDo队列出错", ex);
                return false;
            }
        }

        /// <summary>
        /// 添加内容类型数据添加到ToDo队列
        /// </summary>
        /// <param name="content">内容</param>
        /// <param name="flag">内容标志</param>
        /// <param name="route">路由标志</param>
        /// <returns></returns>
        public static bool AddContentToTodoQueue(string[] content,string flag,string route)
        {
            try
            {
                foreach (var c in content)
                {
                    if (!string.IsNullOrWhiteSpace(c))
                    {
                        ToDoData urData = new ToDoData();
                        urData.Obj = c;
                        urData.Flag = flag;
                        urData.Route = route;
                        ToDo.AddUrl(urData);
                    }

                }
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("添加内容类型数据添加到ToDo队列出错", ex);
                return false;
            }
        }
        /// <summary>
        /// 获取爬虫队列中的URLs
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllToTodoQueueUrls()
        {
            List<string> list=new List<string>();
            var quen=ToDo.GetAll();
            foreach (ToDoData toDoData in quen)
            {
                string url = toDoData.PageUrl.ToLower();
                list.Add(url);
            }
            return list.Distinct().ToList();
        }
        /// <summary>
        /// 获取爬虫队列中的URLs
        /// </summary>
        /// <returns></returns>
        public static void GetAllToTodoQueueUrls(List<string> urls )
        {
            foreach (string url in urls)
            {
                ToDo.AddUrl(new ToDoData()
                {
                    
                });
            }
        }
        /// <summary>
        /// 命令蜘蛛队长开始工作
        /// </summary>
        /// <returns></returns>
        public static bool CommandSpiderToWork()
        {
            try
            {
                Thread th = new Thread(SpiderCaptain.GetInstance().StartToWork);
                th.Start();
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("命令蜘蛛队长开始工作出错", ex);
                return false;
            }
        }
        /// <summary>
        /// 命令蜘蛛队长开始工作，,单线程模型
        /// </summary>
        /// <returns></returns>
        public static bool CommandSpiderToWorkBySTA(WebBrowser browser=null)
        {
            try
            {
                SpiderCaptain.GetInstance().StartToWorkBySTA(browser);
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("命令蜘蛛队长开始工作出错", ex);
                return false;
            }
        }
        /// <summary>
        /// 命令蜘蛛队长停止工作
        /// </summary>
        /// <returns></returns>
        public static bool CommandSpiderStopToWork(string reason = "")
        {
            try
            {
                SpiderCaptain.GetInstance().StopToWork(reason);
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("命令蜘蛛队长停止工作出错", ex);
                return false;
            }
        }
        /// <summary>
        /// 设置爬虫队长工作速度
        /// </summary>
        /// <param name="speed"></param>
        public static void SetSpriderCaptainWorkSpeed(int speed)
        {
            SpiderCaptain.WorkSpeed = speed;
        }
       
        /// <summary>
        /// 设置爬虫WebBrowser
        /// </summary>
        /// <param name="browser"></param>
        public static void SetSpriderWebBrowser(WebBrowser browser)
        {
            SpiderCore.Spiders.Spider.WebBrowser = browser;
        }
        /// <summary>
        /// 设置爬虫WebBrowser延时
        /// </summary>
        /// <param name="second"></param>
        public static void SetSpriderWebBrowserDelay(int second)
        {
            SpiderCore.Spiders.Spider.WebBrowserDelay = second;
        }
        /// <summary>
        /// 清除IE浏览器缓存
        /// </summary>
        public static void ClearBrowserFileByRunCmd()
        {
           SpiderHelper.ClearBrowserFileByRunCmd();
        }
        /// <summary>
        /// 重置爬虫系统
        /// </summary>
        public static void ReSetSpiderConfig()
        {
            SpiderCaptain.EndTime = DateTime.MinValue;
            SpiderCaptain.StopWorkReason ="";
        }
        /// <summary>
        /// 设置访问记录表中Url为已经访问成功
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        internal static bool SetVisitedUrlSuccess(string url)
        {
            DicVisited visited = new DicVisited();
            try
            {
                var data = visited.GetUrl(url);
                if (data != null)
                {
                    data.IsSucce = true;
                }
                return true;
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog(" 设置访问记录表中Url为已经访问成功出错", ex);
                return false;
            }

        }
        /// <summary>
        /// 设置Url任务的当前状态
        /// </summary>
        /// <param name="url"></param>
        /// <param name="state"></param>
        internal static void SetUrlTaskState(string url, string state)
        {
            try
            {
                SpiderMonitor.SetUrlTaskState(url, state);
            }
            catch (Exception ex)
            {
                CustomsLog.WriteExceptionErrorLog("设置Url任务的当前状态出错", ex);
            }

        }
    }
}
